import { defineStore } from 'pinia';
import { ref } from 'vue';
import request from '@/utils/request';
import { http } from '@/utils/request';
import type {
  CategoryList,
  bannerList,
  newsArticlesList,
  hotRecommendsList,
} from '@/types';

export const useHomeStore = defineStore('home', {
  // 开启本地化存储,所有数据持久化
  persist: true,
  // 按需开启本地存储
  // persist: {
  //   // 修改存储中使用的键名称，默认为当前 Store的 id
  //   key: 'storekey',
  //   // 修改为 sessionStorage，默认为 localStorage
  //   storage: window.sessionStorage,
  //   // 🎉按需持久化，默认不写会存储全部
  //   paths: ['categoryList', 'homeBanner'],
  // },
  state: () => {
    return {
      categoryList: [] as CategoryList,
      homeBanner: [] as bannerList,
      newsArticles: [] as newsArticlesList,
      hotRecommends: [] as hotRecommendsList,
    };
  },
  actions: {
    // 获取分类数据
    async getAllCategory() {
      const res = await http<CategoryList>('get', '/home/category/head');
      // console.log(res.data.result);
      this.categoryList = res.data.result;
      // console.log(this.categoryList);
    },

    // 获取banner数据
    async getBanner() {
      const res = await http<bannerList>('get', '/home/banner');
      this.homeBanner = res.data.result;
      console.log(this.homeBanner, 'homeBanner');
    },

    // 获取新鲜好物数据
    async getNewArticles() {
      const Newres = await http<newsArticlesList>('get', '/home/new');

      this.newsArticles = Newres.data.result;
      console.log(this.newsArticles, '新鲜好物');
    },

    // 获取人气推荐数据
    async getHotList() {
      const hot = await request.get('/home/hot');
      this.hotRecommends = hot.data.result;
      console.log(this.hotRecommends, '人气推荐');
    },
  },
});
